package api.apiservice.apiopenplat.warpper;

import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HtmlUtil;
import cn.hutool.json.JSONUtil;

import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.Map;

/*

* 对HTTP中的参数进行过滤（其实就是对标签进行过滤 ）

**/
public class xssHttpServletRequestWrapper extends HttpServletRequestWrapper {
    public xssHttpServletRequestWrapper(HttpServletRequest request) {
        super(request);
    }

    @Override
    public String getParameter(String name) {
        String value = super.getParameter(name);
        if (!StrUtil.isEmpty(value)){
            value = HtmlUtil.filter(value);
        }
        return value;
    }


    @Override
    public String[] getParameterValues(String name) {
        String[] values = super.getParameterValues(name);
        if (values.length!=0) {
            for (int i = 0; i < values.length; i++) {
                if (!StrUtil.isEmpty(values[i])) {
                    values[i] = HtmlUtil.filter(values[i]);
                }
            }
        }
        return values;
    }

    @Override
    public Map<String, String[]> getParameterMap() {
        Map<String, String[]> Valuse = super.getParameterMap();
        if (Valuse != null){
            Valuse.forEach((k, v) -> {
                for (int i = 0; i < v.length; i++) {
                    if (!StrUtil.isEmpty(v[i])) {
                        v[i] = HtmlUtil.filter(v[i]);
                    }
                }
            });
        }
        return Valuse;
    }

    @Override
    public String getHeader(String name) {
        String header = super.getHeader(name);
        if (!StrUtil.isEmpty(header)) {
            header = HtmlUtil.filter(header);
        }
        return header;
    }

    @Override
    public ServletInputStream getInputStream() throws IOException {
        ServletInputStream inputStream = super.getInputStream();
        InputStreamReader reader = new InputStreamReader(inputStream, Charset.forName("UTF-8"));
        BufferedReader bReader = new BufferedReader(reader);
        StringBuffer body = new StringBuffer();
        String line = bReader.readLine();
        while (line != null){
            body.append(line);
            line = bReader.readLine();
        }
        bReader.close();
        reader.close();
        inputStream.close();
        //格式转换
        Map<String,Object> map = JSONUtil.parseObj(body.toString());
        map.forEach((k, v) -> {
            if (v instanceof String){
                if (!StrUtil.isEmpty(v.toString())){
                    v = HtmlUtil.filter(v.toString());
                }
            }
        });
        String s = JSONUtil.toJsonStr(map);
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(s.getBytes());
        return new ServletInputStream() {
            @Override
            public boolean isFinished() {
                return false;
            }

            @Override
            public boolean isReady() {
                return false;
            }

            @Override
            public void setReadListener(ReadListener readListener) {

            }

            @Override
            public int read() throws IOException {
                return byteArrayInputStream.read();
            }
        };
    }
}
